"""
Case Type   : 资源池化
Case Name   : gs_collector 收集日志信息指定收集节点ip
Create At   : 2024.01
Owner       : chen
Description :
    指定收集主机名称: gs_collector --begin-time="{date} --end-time="{date}" -h ip
Expect      :
    收集失败
"""

import os
import unittest
import datetime
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.pri_node = Node('PrimaryDbUser')
        self.tmp_path = os.path.join(os.path.dirname(macro.PG_LOG_PATH))


    def test_shared_storage(self):
        text = '-----step1: 指定收集主机名称: gs_collector --begin-time="\{date\}" '\
            '--end-time="\{date\}" -h ip' \
            ' expect: 收集失败-----'
        self.log.info(text)

        self.log.info('-----查看主机ip-----')
        db_host = self.pri_node.db_host
        self.log.info(db_host)

        self.log.info('-----收集日志信息-----')
        current_time = datetime.datetime.now()
        start_time = current_time.strftime('%Y%m%d %H:%M')
        end_time = (current_time + datetime.timedelta(minutes=5)).strftime('%Y%m%d %H:%M')

        self.log.info('-----收集日志信息指定备节点-----')
        collector_cmd = f'source {macro.DB_ENV_PATH};' \
                        f'gs_collector --begin-time="{start_time}" ' \
                        f'--end-time="{end_time}" -h {db_host}'
        self.log.info(collector_cmd)
        res = self.pri_node.sh(collector_cmd).result()
        self.log.info(res)
        self.assertIn(f'The host name [{db_host}] is not in the cluster.', res)

    def tearDown(self):
        self.log.info('-----step2:清理环境-----')
        rm_cmd = f'''cd {self.tmp_path}; rm -rf collector_*'''
        self.log.info(rm_cmd)
        res = self.pri_node.sh(rm_cmd).result()
        self.log.info(res)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')